<?php

namespace app\portal\controller;

use addons\wechat\model\WechatCaptcha;
use app\common\controller\Frontend;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\Attachment;
use think\Config;
use think\Cookie;
use think\Hook;
use think\Session;
use think\Validate;

/**
 * 会员中心
 */
class Pay extends Frontend
{
    protected $layout = 'default';
    protected $noNeedLogin = ['index'];
    protected $noNeedRight = ['*'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $auth = $this->auth;

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

        //监听注册登录退出的事件
        Hook::add('user_login_successed', function ($user) use ($auth) {
            $expire = input('post.keeplogin') ? 30 * 86400 : 0;
            Cookie::set('uid', $user->id, $expire);
            Cookie::set('token', $auth->getToken(), $expire);
        });
        Hook::add('user_register_successed', function ($user) use ($auth) {
            Cookie::set('uid', $user->id);
            Cookie::set('token', $auth->getToken());
        });
        Hook::add('user_delete_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
        Hook::add('user_logout_successed', function ($user) use ($auth) {
            Cookie::delete('uid');
            Cookie::delete('token');
        });
        $this->model = new \app\admin\model\yexam\Subject;
    }
    /**
     * 注册会员
     */
    public function index()
    {
		$url = $this->request->request('url', '', 'trim');
        $source = $this->request->request('source', '', 'trim');
        $name = $this->request->request('name', '', 'trim');
        $ename = $this->request->request('ename', '', 'trim');
        $cn_name = '';
		$row = $package_content = [];
        if($name){
            $row = $this->model->get(['en_name'=>$name]);
            if($row) $cn_name = $row['short_name'];
			$ename = $name;
        }elseif($ename){
			$row = $this->model->get(['en_name'=>$ename]);
            if($row) $cn_name = $row['short_name'];
		}
		if($row){
			$subject = $this->model->get($row['id']);
			$package_ids = $subject->package_ids;
			$item = $subject->toArray();

			$package = new \app\admin\model\Package;

			// content_json to content
			$package_content = [];
			if ($item['package_ids']) {
				$package_ids = explode(',', $item['package_ids']);
				$package_content = collection($package->where('id', 'in', $package_ids)->select())->toArray();
			}

		}
        $client = cmf_is_mobile() ? 'm' : 'pc';
        $this->view->assign('package_content', $package_content);
        $this->view->assign('url', $url);
        $this->view->assign('name', $name);
		$this->view->assign('ename', $ename);
        $this->view->assign('cn_name', $cn_name);
        $this->view->assign('source', $source);
        $this->view->assign('title', __('模拟试题与答案/在线题库APP下载'), $source);
        return $this->view->fetch();
    }
	
	public function oldindex()
    {
		$url = $this->request->request('url', '', 'trim');
        $source = $this->request->request('source', '', 'trim');
		$name = $this->request->request('name', '', 'trim');
        $ename = $this->request->request('ename', '', 'trim');
        $cn_name = '';
        if($ename){
            $row = $this->model->get(['en_name'=>$ename]);
            if($row) $cn_name = $row['short_name'];
        }
        $client = cmf_is_mobile() ? 'm' : 'pc';
        $this->view->assign('url', $url);
        $this->view->assign('ename', $ename);
        $this->view->assign('cn_name', $cn_name);
        $this->view->assign('source', $source);
        $this->view->assign('title', __('牛课宝典体验'));
        return $this->view->fetch();
    }
}
